package com.jintoufs.weixin.order;

import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.interceptor.ParamCheck;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.image.ImageService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.weixin.SupportAction;
import io.swagger.annotations.Api;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "订单相关接口")
@RestController
@RequestMapping("/api/order")
public class OrderAction extends SupportAction {

    @Resource
    private ImageService imageService;
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private SceneOrderService sceneOrderService;
    @Resource
    private TourRouteOrderService tourRouteOrderService;
    @Resource
    private CustomerService customerService;
    @GetMapping("/getOrderList")
    public AjaxMsg getOrderList(String type,Integer pageSize,Integer pageNum,String status,String token){
        Customer customer = getCustomerByToken(token);
        AjaxMsg ajaxMsg = new AjaxMsg();
        if (customer==null){
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("用户验证出现了问题，请重新登陆");
        }else {
            Map<String,Object> params = new HashMap<>();
            //放入分页
            Integer pageStart = (pageNum - 1) * pageSize;
            params.put("pageStart", pageStart);
            params.put("pageSize", pageSize);
            params.put("display", Constants.DISPLAY);
            params.put("customerId",customer.getId());
            if(!ValidUtil.isEmpty(status)){
                params.put("status",status);
            }
            List list = new ArrayList<>();
            switch (type){
                case Constants.ORDER_TYPE_HOTEL:
                    list = hotelOrderService.selectByParam(params);
                    break;
                case Constants.ORDER_TYPE_SCENE:
                    list = sceneOrderService.getByParam(params);
                    break;
                case Constants.ORDER_TYPE_TOUR:
                    list = tourRouteOrderService.getOrderList(params);
                default:
                    break;
            }
            ajaxMsg.setDatas(list);
        }
        return ajaxMsg;
    }

    @GetMapping("/detail")
    public AjaxMsg getDetail(String id,String type){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map<String,Object> orderDetail = null;
        List<Map<String,Object>> customerInfoList = null;
        try {
            Map params = new HashMap(1);
            params.put("id",id);
            switch (type) {
                case Constants.ORDER_TYPE_HOTEL:
                    orderDetail = (Map<String, Object>) hotelOrderService.selectByParam(params).get(0);
                    break;
                case Constants.ORDER_TYPE_SCENE:
                    orderDetail = (Map<String, Object>) sceneOrderService.getByParam(params).get(0);
                    break;
                case Constants.ORDER_TYPE_TOUR:
                    orderDetail = (Map<String, Object>) tourRouteOrderService.getOrderList(params).get(0);
                    break;
                default:
                    break;
            }
            params.clear();
            params.put("orderId", id);
            params.put("type", type);
            customerInfoList = hotelOrderService.getAllTypeOrderCustomerInfoDetail(params);
            orderDetail.put("customerInfoList", customerInfoList);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
            Date createTime = (Date)orderDetail.get("inTime");

            orderDetail.put("createTime", dateFormat.format(createTime));
            if (Constants.ORDER_TYPE_SCENE.equals(type)) {
                orderDetail.put("useDateStr", orderDetail.get("useDate"));
            }else{
                Date useDate = (Date) orderDetail.get("useDate");
                orderDetail.put("useDateStr", dateFormat2.format(useDate));
            } if (Constants.ORDER_TYPE_HOTEL.equals(type)) {
                orderDetail.put("outTimeStr", dateFormat2.format(orderDetail.get("check_out_time")));
            }
            ajaxMsg.setDatas(orderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
        }
        return ajaxMsg;
    }
    @GetMapping("/deleteOrder")
    public AjaxMsg deleteOrder(String id,String type){
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            Map params = new HashMap(1);
            params.put("id",id);
            switch (type) {
                case Constants.ORDER_TYPE_HOTEL:
                   HotelOrder hotelOrder= hotelOrderService.getById(id);
                    hotelOrder.setDisplay(Constants.DISPLAY_DEL);
                    hotelOrderService.updateOrder(hotelOrder);
                    break;
                case Constants.ORDER_TYPE_SCENE:
                    SceneOrder sceneOrder= sceneOrderService.getById(id);
                    sceneOrder.setDisplay(Constants.DISPLAY_DEL);
                    sceneOrderService.updateOrder(sceneOrder);
                    break;
                case Constants.ORDER_TYPE_TOUR:
                    TourRouteOrder  tourRouteOrder= tourRouteOrderService.getById(id);
                    tourRouteOrder.setDisplay(Constants.DISPLAY_DEL);
                    tourRouteOrderService.update(tourRouteOrder);
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
        }
        return ajaxMsg;
    }


    /**
     * 退款
     * @param type
     * @param orderId
     * @param token
     * @return
     */
    @GetMapping("/roll")
    @ResponseBody
    public AjaxMsg roll(String type,String orderId,String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (customer==null){
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("用户验证出现了问题，请重新登陆");
        }else {
            switch (type){
                case Constants.ORDER_TYPE_HOTEL:
                    HotelOrder hotelOrder = hotelOrderService.getById(orderId);
                    hotelOrder.setStatus(Constants.ORDER_STATUS_ROLLING);
                    //设置为未读订单
                    hotelOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    hotelOrderService.updateOrder(hotelOrder);
                    break;
                case Constants.ORDER_TYPE_SCENE:
                    SceneOrder sceneOrder = sceneOrderService.getById(orderId);
                    sceneOrder.setStatus(Constants.ORDER_STATUS_ROLLING);
                    //设置为未读订单
                    sceneOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    sceneOrderService.updateOrder(sceneOrder);
                    break;
                case Constants.ORDER_TYPE_TOUR:
                    TourRouteOrder tourRouteOrder = tourRouteOrderService.getById(orderId);
                    tourRouteOrder.setStatus(Constants.ORDER_STATUS_ROLLING);
                    //设置为未读订单
                    tourRouteOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    tourRouteOrderService.update(tourRouteOrder);
                default:break;
            }
        }
        return ajaxMsg;
    }

    /**
     * 取消订单
     * @param id
     * @param token
     * @param type
     * @return
     */
    @GetMapping("/cancel")
    @ParamCheck(params = "type,id,token")
    @ResponseBody
    public AjaxMsg cancelOrder(String id,String token,String type){
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            //验证token
            Customer customer = customerService.getByToken(token);
            if (ValidUtil.isEmpty(customer)) {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("用户验证失败");
                return  ajaxMsg;
            }
            switch (type) {
                case Constants.ORDER_TYPE_HOTEL:
                    HotelOrder hotelOrder = hotelOrderService.getById(id);
                    if (!hotelOrder.getStatus().equals(Constants.ORDER_STATUS_UNPAY)){
                        ajaxMsg.setCode(AjaxMsg.FAILURE);
                        ajaxMsg.setMessage("订单已确认或已取消");
                        return  ajaxMsg;
                    }
                    hotelOrder.setStatus(Constants.ORDER_STATUS_CANCEL);
                    hotelOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    hotelOrderService.updateOrder(hotelOrder);
                    break;
                case Constants.ORDER_TYPE_SCENE:
                    SceneOrder sceneOrder = sceneOrderService.getById(id);
                    if (!sceneOrder.getStatus().equals(Constants.ORDER_STATUS_UNPAY)){
                        ajaxMsg.setCode(AjaxMsg.FAILURE);
                        ajaxMsg.setMessage("订单已确认或已取消");
                        return  ajaxMsg;
                    }
                    sceneOrder.setStatus(Constants.ORDER_STATUS_CANCEL);
                    sceneOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    sceneOrderService.updateOrder(sceneOrder);
                    break;
                case Constants.ORDER_TYPE_TOUR:
                    TourRouteOrder tourRouteOrder = tourRouteOrderService.getById(id);
                    if (!tourRouteOrder.getStatus().equals(Constants.ORDER_STATUS_UNPAY)){
                        ajaxMsg.setCode(AjaxMsg.FAILURE);
                        ajaxMsg.setMessage("订单已确认或已取消");
                        return  ajaxMsg;
                    }
                    tourRouteOrder.setStatus(Constants.ORDER_STATUS_CANCEL);
                    tourRouteOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                    tourRouteOrderService.update(tourRouteOrder);
                    break;
                default:
                    break;
            }
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
            ajaxMsg.setMessage("操作成功");
            return  ajaxMsg;
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
        }
        return  ajaxMsg;
    }


}
